package com.hgl.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sun.util.logging.resources.logging;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;

import com.hgl.dao.DAOiml;
import com.hgl.dao.HglDAO;
import com.hgl.dto.HglDTO;
import com.hgl.entity.Hgl;
import com.hgl.service.HglService;
import com.hgl.vo.HglVO;

/**
 * 
 * @author:HGL
 * ClassName:com.hgl.service.impl.HglServiceImpl.java
 * Date:2020年7月24日下午2:45:08
 * Version:1.0
 */
public class HglServiceImpl implements HglService {
	private static Logger log = LoggerFactory.getLogger(HglServiceImpl.class);
    
	private HglDAO hglDAO = new DAOiml();

	@Override
	public List<HglVO> list() {
		List<HglVO> list = new ArrayList<HglVO>();
		List<Hgl> list1 = hglDAO.selectALL();
		list1.forEach((entity) -> {
			HglVO vo = new HglVO();
			BeanUtil.copyProperties(entity, vo);
			list.add(vo);
		});
		return list;
	}

	
	
	@Override
	public int add(Hgl mb) {
	if(StrUtil.hasBlank(mb.getHglName())){
		throw  new RuntimeException("账户名不能为空");
	}
	if(StrUtil.hasBlank(mb.getHglTel())){
		throw new RuntimeException("电话号码不能为空");
	}

	
	try{
		return hglDAO.add(mb);
	}catch(Exception ex){
		ex.printStackTrace();
		throw new RuntimeException("保存用户数据失败", ex);
	}
		
	}

	@Override
	public int deleteById(int id) {
		try {

			return hglDAO.deleteById(id);
		} catch (Exception ex) {
			throw new RuntimeException("删除用户失败", ex);
		}
	}

	@Override
	public List<Hgl> selectById(int id) {
		try {
			List<Hgl> list = new ArrayList<Hgl>();
			List<Hgl> list1 = hglDAO.selectById(id);
			list1.forEach((entity) -> {
				Hgl vo = new Hgl();
				BeanUtil.copyProperties(entity, vo);
				list.add(vo);
			});
			return list;
		} catch (Exception ex) {
			throw new RuntimeException("查找用户失败", ex);
		}
	}



	@Override
	public boolean existsUserName(String UserName) {
		Hgl hgl=new Hgl();
		hgl.setHglName(UserName);
		return true;
	}



	@Override
	public void add(HglDTO HglDTO) {
		// TODO Auto-generated method stub
		
	}



	


	@Override
	public Hgl get(Integer hglId) {
		Hgl hgl=new Hgl();
		return hgl=hglDAO.selectByid(hglId);
	}



	@Override
	public List<HglVO> selectBylen(int len) {
		try {
			List<HglVO> list = new ArrayList<HglVO>();
			List<Hgl> list1 = hglDAO.selectBylen(len);
			list1.forEach((entity) -> {
				HglVO vo = new HglVO();
				BeanUtil.copyProperties(entity, vo);
				list.add(vo);
			});
			return list;
		} catch (Exception ex) {
			throw new RuntimeException("查找用户失败", ex);
		}
	}



	@Override
	public int update(Hgl hgl) {
	
	return hglDAO.updateById(hgl); 
//		try{
//		hgl.setHglId(null);
//		hgl.setHglName(null);
//		
//		return hglDAO.updateById(hgl, id);
//		}catch(Exception ex){
//			ex.printStackTrace();//日志
//			throw new RuntimeException("修改用户数据失败",ex);
//		}
	}



	@Override
	public int updateById(Hgl mb) {
		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	public List<Hgl> list(int current, int rowMax) {
		
			try {
				return hglDAO.paging((current - 1) * rowMax, rowMax);
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new RuntimeException("分页失败", ex);
			}
		
		
	}



	@Override
	public int count() {
		try{
		return hglDAO.countALL();
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("获取分页数据失败",e);
		}
	}



	@Override
	public List<Hgl> search(String name) {
		return hglDAO.selectByName(name);
	}



	

	// @Override
	// public List<ArticleVO> list() {
	// List<ArticleVO> list = new ArrayList<ArticleVO>();
	// try {
	// List<Article> list1 = articleDAO.selectByModel(null, 0, 10);
	// list1.forEach((entity)->{
	// ArticleVO vo = new ArticleVO();
	// //vo.setArticleId(entity.getArticleId());
	// //...
	// BeanUtil.copyProperties(entity, vo);
	// list.add(vo);
	// });
	//
	// } catch (SQLException e) {
	// e.printStackTrace();//以后禁止使用错误打印和System.out。日志
	// }
	// return list;
	// }
	//

}
